<template>
  <div id='entrepot_list'>
    <div class="search-box">
      <Row type="flex">
        <Row type="flex" justify="start" class="search-item-left">
          <Col class="search-item"><span class="search-label">输入搜索：</span><Input placeholder="仓库名称/仓库缩写" v-model='searchName'></Input></Col>
          <Col class="search-item">
            <Button class="search-btn" type='primary' @click="doSearch">查询</Button>
          </Col>
          <Col class="search-item">
            <Button class="search-btn" @click="reset">重置</Button>
          </Col>
        </Row>
        <Row type="flex" justify="end" class="search-item-right">
          <Col class="search-item"><Button class="search-btn" type="primary" style="margin-right:8px" @click="skipAdd" v-if="hasPermission('moms:scs:wharehousecreate')">添加仓库</Button></Col>
        </Row>
      </Row>
    </div>
    <Table stripe class="table-list" :columns="columns" :data="tableData" :loading="loading" :height="tableHeight">
      <template slot-scope="{ row }" slot="showFlag">
        <span>{{row.whStatus === 1 ? '显示' : row.whStatus === 2 ? '隐藏': ''}}</span>
      </template>
      <template slot-scope="{ row }" slot="action">
        <i class="iconsys icondetail" title="查看" @click='skipDetail(row)'></i>
        <i class="iconsys iconedit" title="编辑" @click='skipEdit(row)'></i>
      </template>
    </Table>
    <Row type='flex' justify="end" style='padding: 20px'>
      <Col>
        <Page :total="pageObj.total"
              :page-size='pageObj.pageSize'
              :current='pageObj.pageIndex'
              :page-size-opts= '[10, 20, 50, 100]'
              show-total
              show-sizer
              @on-change='changePageIndex'
              @on-page-size-change='changePageSize' />
      </Col>
    </Row>
  </div>
</template>

<script>
import { entrepotList } from '@/api/purchase/index'
export default {
  name: 'entrepot_list',
  watch: {
    '$route' (newRoute) {
      if (newRoute.name === 'entrepot_list') {
        this.getData()
      }
    }
  },
  computed: {
    tableHeight () {
      let height = this.tableData.length > 10 ? 520 : 0
      return height
    }
  },
  data () {
    return {
      loading: false,
      searchName: '',
      pageObj: {
        searchName: '',
        pageIndex: 1,
        pageSize: 20,
        total: 0
      },
      tableData: [],
      columns: [
        {
          title: '仓库缩写',
          key: 'whAbbr'
        },
        {
          title: '仓库名称',
          key: 'whName'
        },
        {
          title: '所属部门/门店',
          key: 'orgName'
        },
        {
          title: '是否显示',
          slot: 'showFlag'
        }
      ]
    }
  },
  mounted () {
    this.getData()
    if (this.hasPermission(['moms:scs:wharehousedetail', 'moms:scs:wharehouseupdate'])) {
      this.columns.push({
        title: '操作',
        slot: 'action',
        width: 200
      })
    }
  },
  methods: {
    reset () {
      this.searchName = ''
      this.pageObj.searchName = ''
      this.pageObj.pageIndex = 1
      this.pageObj.pageSize = 20
      this.getData()
    },
    doSearch () {
      this.pageObj.searchName = this.searchName
      this.pageObj.pageIndex = 1
      this.getData()
    },
    skipAdd () {
      this.$router.push({ name: 'entrepot_add' })
    },
    skipDetail (row) {
      this.$router.push({ name: 'entrepot_detail', params: { whCode: row.whCode } })
    },
    skipEdit (row) {
      this.$router.push({ name: 'entrepot_edit', params: { whCode: row.whCode } })
    },
    changePageIndex (index) {
      this.pageObj.pageIndex = index
      this.getData()
    },
    changePageSize (size) {
      this.pageObj.pageSize = size
      this.pageObj.pageIndex = 1
      this.getData()
    },
    getData () {
      this.loading = true
      entrepotList({
        merchantId: this.$store.state.user.merchantId,
        orgGuid: this.$store.state.user.orgId,
        keyword: this.pageObj.searchName,
        offset: this.pageObj.pageSize * (this.pageObj.pageIndex - 1),
        limit: this.pageObj.pageSize,
        displayOrder: 1
      }).then(res => {
        this.loading = false
        const result = res.data || {}
        if (result.status === 200) {
          this.tableData = result.data.records || []
          this.pageObj.total = result.data.total
        } else {
          this.$Message.error(result.error || result.message)
        }
      })
    }
  }
}
</script>
